package com.wl.video.live.api;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.wl.video.live.AppContext;
import com.wl.video.live.entity.LiveChannelEntity;
import com.wl.video.live.entity.LiveLinkEntity;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.message.BasicNameValuePair;

public class LiveAPI {

    public static void getLiveChannel(LiveChannelEntity channelEntity, AsyncHttpResponseHandler handler) {
//        try {
//            JSONObject jsonObject = new JSONObject();
//            //1c505e3e737861c7ffd05e96dcbbee4a
//            jsonObject.put("deviceId", channelEntity.getDeviceId());
//            jsonObject.put("liveDate", channelEntity.getLiveDate());
//            jsonObject.put("liveTime", channelEntity.getLiveTime());
//            jsonObject.put("seePeople", channelEntity.getLiveNum());

//            HashMap<String,String> map = new HashMap<String,String>();
//            //1c505e3e737861c7ffd05e96dcbbee4a
//            map.put("deviceId", channelEntity.getDeviceId());
//            map.put("liveDate", channelEntity.getLiveDate());
//            map.put("liveTime", channelEntity.getLiveTime());
//            map.put("seePeople", channelEntity.getLiveNum());

        RequestParams rs = new RequestParams();
        rs.put("deviceId", channelEntity.getDeviceId());
        rs.put("liveDate", channelEntity.getLiveDate());
        rs.put("liveTime", channelEntity.getLiveTime());
        rs.put("seePeople", channelEntity.getLiveNum());

//            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//            for (Map.Entry<String, String> entry : map.entrySet()) {
//                nvps.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
//            }
//            UrlEncodedFormEntity se = new UrlEncodedFormEntity(nvps, "UTF-8");
//            StringEntity stringEntity = new StringEntity(map.toString(), "UTF-8");
//            ApiHttpClient.post(AppContext.context(), "/api/channel/createChannel", se, "text/xml", handler);
        ApiHttpClient.post("/api/channel/createChannel", rs, handler);

//        }  catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }
    }

    public static void getLiveLink(LiveLinkEntity liveLinkEntity, AsyncHttpResponseHandler handler) {
        RequestParams rs = new RequestParams();
        rs.put("deviceId", liveLinkEntity.getDeviceId());
        ApiHttpClient.post("/api/channel/startChannel", rs, handler);

    }

    public static void getLiveVideo(String deviceId, AsyncHttpResponseHandler handler) {
        RequestParams rs = new RequestParams();
        rs.put("deviceId", deviceId);
        ApiHttpClient.post("/api/channel/getVideotape", rs, handler);
    }

    public static void updateVideoTag(String videoId, String tagName, AsyncHttpResponseHandler handler) {

        RequestParams rs = new RequestParams();
        rs.put("videoId", videoId);
        rs.put("labelName", tagName);

        ApiHttpClient.post("/api/channel/updateVideoLabel", rs, handler);
    }

    public static void genCode(String deviceId, String code, AsyncHttpResponseHandler handler) {

        RequestParams rs = new RequestParams();
        rs.put("deviceId", deviceId);
        rs.put("code", code);

        ApiHttpClient.post("/api/channel/updateLiveChannelCode", rs, handler);
    }

}
